package com.gec.service.Impl;

import com.gec.dao.UserDao;
import com.gec.dao.UserRoleDao;
import com.gec.domain.PageBean;
import com.gec.domain.PageParam;
import com.gec.domain.User;
import com.gec.service.abst.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends BaseService implements UserService {
    private final UserDao userDao;
    private final UserRoleDao userRoleDao;

    public UserServiceImpl(UserDao userdao, UserRoleDao userRoleDao) {
        this.userDao = userdao;
        this.userRoleDao = userRoleDao;
    }


    @Override
    public User getUserByAccount(String account, String password) {
        User user = userDao.getUserByAccount(account);
        System.out.println("+----------------------------+");
        System.out.println(user);
        System.out.println("+----------------------------+");
        if (user == null) {
            throw new RuntimeException("用户登录失败");
        }
        String ps = user.getPassword();
        if (!ps.equals(password)) {
            throw new RuntimeException("用户登录失败");
        }
        return user;
    }

    @Override
    public PageBean<User> getUserList(PageParam param) {
        int size = param.getLimit();  //页大小
        int page = param.getPage();   //页码
        //{1}在查询之前先调用这个方法来准备拦截查询
        Page<Object> pageInfo = PageHelper.startPage(page, size, true);
        //{2}调用 dao 方法来查询
        List<User> list = userDao.getUserList(param.getKeywords());
        PageBean pBean = new PageBean();
        int total = (int) pageInfo.getTotal();
        //{3}生成 PageBean, 再封装信息到 pBean
        pBean.setCount(total);  //总记录数
        pBean.setData(list);    //用户列表
        pBean.setCode(0);     //状态码
        return pBean;
    }

    @Override
    public void saveUser(User user) throws Exception {
        String id = user.getId();
        try {
            if (id != null) { // id!=null --> 更新
                String password = user.getPassword();
                System.out.println(password);
                Md5Hash newPssword = new Md5Hash(password, null, 5);
                System.out.println(newPssword);
                user.setPassword(newPssword.toString());
                if(!"".equals(user.getRoleId())) userRoleDao.updateUserRole(user.getRoleId(), user.getId());
                userDao.updateUser(user);
            } else { // id==null --> 添加
                String password = user.getPassword();
                System.out.println(password);
                Md5Hash newPssword = new Md5Hash(password, null, 5);
                System.out.println(newPssword);
                user.setPassword(newPssword.toString());
                String roleId = user.getRoleId();
                String uuid = uuid();
                userRoleDao.addUserRole(uuid, roleId);
                user.setId(uuid); // 设置 uuid
                System.out.println(user);
                userDao.addUser(user);
            }
        } catch (Exception e) {
            String[][] check = {{"uni_account", "用户帐户重复"}, {"uni_no", "用户工号重复"}};
            // {ps}在这里重检测异常, 转换一个新异常再来。
            checkUniqueException(e, check);
        }
    }


    @Override
    public void delUser(String id) {
        userDao.delUser(id);
        userRoleDao.deleteUserRole(id);
    }


    @Override
    public User getUserById(String id) {
        User user = userDao.getUserById(id);
        System.out.println("+----------------------------+");
        System.out.println(user);
        System.out.println("+----------------------------+");
        return user;
    }

}
